//
//  DMLogInController.m
//  DMGME
//
//  Created by ihope99 on 15/9/7.
//  Copyright (c) 2015年 GYP. All rights reserved.
//

#import "DMLogInController.h"
#import "DMRegistAController.h"
#import "ExamineBController.h"
#import "DMHomeViewController.h"
#import "DMForgetViewController.h"
@interface DMLogInController ()<UIAlertViewDelegate>
@property (nonatomic,copy)NSString *orderTotalStr;
@property (nonatomic,copy)NSString *subsidyTotalStr;
@property (nonatomic,copy)NSString *filepath;
@end

@implementation DMLogInController
-(void)viewWillAppear:(BOOL)animated
{
    [self CheckandupdateRequest];
}
-(void)CheckandupdateRequest
{
    NSMutableDictionary *dict1 = [NSMutableDictionary dictionary];
    [dict1 setValue:[AFDownManager MD5String:@"%^&*dongmangoug!@#$"] forKey:@"token"];
    [dict1 setValue:@"2" forKey:@"system_type"];
    [dict1 setValue:@"2" forKey:@"app_type"];
    
    
    [AFDownManager POST:CheckUpdate parameters:dict1 success:^(id responseObject1) {
       
//        NSLog(@"更新 === %@",responseObject1);
        if ([responseObject1[@"data"] count] != 0) {
            
       
        if (![responseObject1[@"data"][@"vision"] isEqualToString:[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"]])
        {
            UIAlertView *createUserResponseAlert = [[UIAlertView alloc] initWithTitle:@"新版本" message:responseObject1 [@"data"][@"description"] delegate:self cancelButtonTitle:@"取消" otherButtonTitles: @"App Store", nil];
            [createUserResponseAlert show];
                }
             _filepath = responseObject1[@"data"][@"filepath"];
             }
       
    } failure:^(NSError *error) {

        [self showMsg:@"网络连接故障"];
//        NSLog(@"error  == %@",error);
    }];
    
//    NSLog(@"版本号 === %@",[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"]);
}
- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    // the user clicked one of the OK/Cancel buttons
    if (buttonIndex == 1)
    {
//        NSString *iTunesLink = @"https://itunes.apple.com/us/app/you-le-jie/id1049294454?l=zh&ls=1&mt=8";
//        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:iTunesLink]];
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitle:@"登录"];
    //NSLog(@"%@",[AFDownManager GetUserID]);
    self.view.backgroundColor = [UIColor whiteColor];
//    [self AddLeftImageBtn:[UIImage imageNamed:@"fanhui_03 2"]target:self action:@selector(GoBack)];
    [_okBtn addTarget:self action:@selector(okBtnLogInClick) forControlEvents:UIControlEventTouchUpInside];
    [_registBtn addTarget:self action:@selector(registBtnLogInClick) forControlEvents:UIControlEventTouchUpInside];
    [_forgetBtn addTarget:self action:@selector(forgetBtnClick) forControlEvents:UIControlEventTouchUpInside];
   
    _phoneNumTextF.inputAccessoryView = [self GetInputAccessoryView];
    _phoneNumTextF.keyboardType = UIKeyboardTypeNumberPad;
    _passNumTextF.inputAccessoryView = [self GetInputAccessoryView];
    _passNumTextF.secureTextEntry = YES;
    

}
- (void)registBtnLogInClick{
    DMRegistAController *regiVC = [[DMRegistAController alloc]initWithNibName:@"DMRegistAController" bundle:nil];
    [self.navigationController pushViewController:regiVC animated:YES];
}
- (void)okBtnLogInClick
{
    if (!_phoneNumTextF||_phoneNumTextF.text.length == 0) {
        [self showMsg:@"请输入手机号码"];
    }else if (!_passNumTextF||_passNumTextF.text.length == 0) {
        [self showMsg:@"请输入密码"];
    }else{
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        [dict setValue: [AFDownManager MD5String:@"%^&*dongmangoug!@#$"] forKey:@"token"];
        [dict setValue: _phoneNumTextF.text forKey:@"mobile"];
        [dict setValue: _passNumTextF.text forKey:@"password"];
        [self StartLoading];
        [AFDownManager POST:LOGINNET parameters:dict success:^(id responseObject) {
            [self StopLoading];
//            NSLog(@"LOGINNET  == %@",responseObject);

            if ([responseObject[@"code"] intValue] == 0) {
                [self showMsg:@"登录成功"];
               
                [[NSUserDefaults standardUserDefaults] setValue:responseObject[@"data"][@"realname"] forKey:@"realname"];
                [[NSUserDefaults standardUserDefaults] setValue:responseObject[@"data"][@"mobile"] forKey:@"mobile"];
                [[NSUserDefaults standardUserDefaults] setValue:responseObject[@"data"][@"shop"][@"cover"] forKey:@"avatar"];
                [[NSUserDefaults standardUserDefaults] synchronize];
                if ([responseObject[@"data"][@"status"] isEqualToString:@"0"]) {
                   
                    ExamineBController *examin = [[ExamineBController alloc] init];
                    [self.navigationController pushViewController:examin animated:YES
                     ];
                    kkUserDict = nil;
                    kkUserDict = responseObject[@"data"];
                    examin.mMobile = responseObject[@"data"][@"mobile"];
                    examin.mRealname = responseObject[@"data"][@"realname"];
                    examin.mAgentPhone = responseObject[@"data"][@"parentAgent"][@"mobile"];
                    examin.shopId = responseObject[@"data"][@"shop"][@"id"];
                
                    
                }
                else if ([responseObject[@"data"][@"status"] isEqualToString:@"1"]){
                    NSString *mobileId = responseObject[@"data"][@"mobile"];
                    NSMutableDictionary *dict1 = [NSMutableDictionary dictionary];
                    [dict1 setValue:[AFDownManager MD5String:@"%^&*dongmangoug!@#$Agent"] forKey:@"token"];
                    [dict1 setValue:mobileId forKey:@"mobile"];
                    [dict1 setValue:@"Agent" forKey:@"action"];
                    
                    
                    [AFDownManager POST:getToken parameters:dict1 success:^(id responseObject1) {
                        if ([responseObject1[@"desc"]  isEqual: @"SUCCESS"]) {
                            [[NSUserDefaults standardUserDefaults] setObject:responseObject1[@"data"][@"token"] forKey:@"RongYunToken"];
                            [[NSUserDefaults standardUserDefaults] setObject:responseObject[@"data"][@"mobile"] forKey:@"mobile"];
                            
                            [[NSUserDefaults standardUserDefaults] synchronize];
                            [self.view removeFromSuperview];
                            
                            DMHomeViewController *dmHomeVC = [[DMHomeViewController alloc] init];
                            
                            
                            NSUserDefaults *userdefault = [NSUserDefaults standardUserDefaults];
                            [userdefault setObject:responseObject[@"data"][@"openid"] forKey:@"openid"];
                            [userdefault synchronize];
                            
                            UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:dmHomeVC];
                            
                            //通知传RongYunToken
                            NSNotification *notification1 = [NSNotification notificationWithName:@"Login" object:@"YES"];
                            [[NSNotificationCenter defaultCenter]  postNotification:notification1];
                            [self presentViewController:nav animated:YES completion:nil];
                            self.view.window.rootViewController = nav;
                            kkUserDict = nil;
                            kkStatus = @"1" ;
                            kkUserDict = responseObject[@"data"];
                            
                            NSUserDefaults *de = [NSUserDefaults standardUserDefaults];
                            [de setObject:responseObject[@"data"][@"level"] forKey:@"level"];
                            [de synchronize];

                        }
                    } failure:^(NSError *error) {
                        [self showMsg:@"网络连接故障"];
//                        NSLog(@"error  == %@",error);
                    }];
                  
                                   }
            }else {
                [self showMsg:responseObject[@"desc"]];
            }
            
        } failure:^(NSError *error) {
            [self StopLoading];
            [self showMsg:@"请检查网络连接"];
//            NSLog(@"error  == %@",error);
        }];
    }

}
-(void)getTokenForDMGoServer:(NSString *)mobileId
{
    NSMutableDictionary *dict1 = [NSMutableDictionary dictionary];
    [dict1 setValue:[AFDownManager MD5String:@"%^&*dongmangoug!@#$Agent"] forKey:@"token"];
    [dict1 setValue:mobileId forKey:@"mobile"];
    [dict1 setValue:@"Agent" forKey:@"action"];

    
    [AFDownManager POST:getToken parameters:dict1 success:^(id responseObject1) {
        
        if ([responseObject1[@"desc"]  isEqual: @"SUCCESS"]) {
            [[NSUserDefaults standardUserDefaults] setObject:responseObject1[@"data"][@"token"] forKey:@"RongYunToken"];
            [[NSUserDefaults standardUserDefaults] synchronize];
            
        }
    } failure:^(NSError *error) {
        [self showMsg:@"网络连接故障"];
//         NSLog(@"error  == %@",error);
    }];
  
}
-(void)forgetBtnClick
{
    DMForgetViewController *forgetVc = [[DMForgetViewController alloc] init];
    
    [self.navigationController pushViewController:forgetVc animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
